package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tan.model.domain.UserInfo;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.dubbo.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@DubboService
public class UserInfiapiimpl implements UserInfoapi{
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public void save(UserInfo userInfo) {

       this.userInfoMapper.insert(userInfo);
    }

    @Override
    public void updateavatar(long longValue, String url) {
     UserInfo userInfo = new UserInfo();
     userInfo.setAvatar(url);
     userInfo.setId(longValue);
     this.userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo getUserInfo(Long userID) {
       UserInfo userInfo = this.userInfoMapper.selectById(userID);



        return userInfo;
    }

    @Override
    public void updateUserInfo(UserInfo userInfo, long longValue) {
        userInfo.setId(longValue);
        this.userInfoMapper.updateById(userInfo);
    }
    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds, UserInfo info) {
        QueryWrapper qw = new QueryWrapper();
        //1、用户id列表
        qw.in("id",userIds);
        //2、添加筛选条件
        if(info != null) {
            if(info.getAge() != null) {
                qw.lt("age",info.getAge());
            }
            if(!StringUtils.isEmpty(info.getGender())) {
                qw.eq("gender",info.getGender());
            }
        }
        List<UserInfo> list = userInfoMapper.selectList(qw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");
        return map;
    }

    @Override
    public UserInfo searchUserInfo(Long userid) {
        QueryWrapper qw = new QueryWrapper();
      qw.eq("id",userid);
        UserInfo userInfo = userInfoMapper.selectOne(qw);

        return userInfo;
    }

    @Override
    public UserInfo getUserInfokeyword(Long id, String keyword) {
       QueryWrapper qw = new QueryWrapper();
       qw.eq("id",id);
       if(!StringUtils.isEmpty(keyword)){
           qw.like("nickname",keyword);
       }
       UserInfo userInfo = userInfoMapper.selectOne(qw);
       return userInfo;
    }


    @Override
    public IPage findAll(Integer page, Integer pagesize) {
        Page page1 = new Page(page,pagesize);
        IPage iPage = userInfoMapper.selectPage(page1, null);
        if(iPage != null){
            return iPage;
        }
        return null;
    }
}
